package com.skynet.supervision.utils;

import com.skynet.supervision.common.ErrCode;
import com.skynet.supervision.common.Result;
import com.skynet.supervision.entities.UserAccount;
import com.skynet.supervision.enums.UserAccountStatus;
import lombok.SneakyThrows;
import lombok.val;

@SuppressWarnings("unused")
public abstract class AccountUtils {

    /**
     * 检查用户账号状态是否正常
     *
     * @param account 用户账号
     */
    @SneakyThrows
    public static void checkAccountStatusOrThrow(UserAccount account) {
        val status = UserAccountStatus.of(account.getStatus());
        if (!status.equals(UserAccountStatus.NORMAL)) {
            throw Result.failure(ErrCode.USER_STATUS_UN_NORMAL, "账号状态为 {} ", status.getDesc()).wrap();
        }
    }

    @SneakyThrows
    public static void checkPassOrThrow(String want, String pass, String salt) {
        if (!Strings.strcmp(want, passwordEncrypt(pass, salt))) {
            throw Result.failure(ErrCode.USER_PASS_NOT_CORRECT).wrap();
        }
    }

    public static String passwordEncrypt(String pass, String salt) {
        return MD5.hash(Strings.sprintf("{}_{}", salt, pass));
    }
}
